package Ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import Messaging.Notification;
import Messaging.NotificationManager;
import Model.Card;
import Model.Player;

/*
 * @author Paolo Cordova
 */

public class MessagePopUp extends JPanel 
{
	
	static final long serialVersionUID = 102;
	
	public MessagePopUp(String message)
	{  	
		Vector<String> lines = processText(message);
		int numOfLabels = lines.size();
		
		JPanel basePanel = new JPanel();
		JPanel messagePanel = new JPanel();
		JLabel labels[] = new JLabel[numOfLabels];
				
		int hGap = 10, vGap = 10;
		GridLayout gridLayout = new GridLayout(2, 1, hGap, vGap);
		GridLayout gridLayout2 = new GridLayout(numOfLabels, 1, 0, 0);
		
		basePanel.setLayout(null);
		messagePanel.setLayout(null);
		
		Font labelFont = new Font("SansSerif", Font.BOLD, 20);
		Font buttonFont = new Font("SansSerif", Font.BOLD, 18);
		Font titleFont = new Font("SansSerif", Font.BOLD, 14);
		
		
		for (int i = 0; i < numOfLabels; ++i) 
		{
			labels[i]  = new JLabel(lines.elementAt(i));
			labels[i].setFont(labelFont);
			labels[i].setForeground(Color.white);
			messagePanel.add(labels[i]);
		}
		
		JButton ok = new JButton("OK");
		ok.setFont(buttonFont);
		ok.setVisible(true);
		
		basePanel.setLayout(gridLayout);
		basePanel.setBackground(new Color(0, 0, 0));
		messagePanel.setLayout(gridLayout2);
		messagePanel.setBackground(new Color(0, 0, 0));
		
		Border blackline = BorderFactory.createLineBorder(Color.WHITE, 2);
		TitledBorder title = BorderFactory.createTitledBorder(
			       blackline, "  Notice  ");

		title.setTitleColor(Color.white);
		title.setTitleFont(titleFont);
		title.setTitleJustification(TitledBorder.LEFT);
		basePanel.setBorder(title);
		basePanel.add(messagePanel);
		basePanel.add(ok);
		basePanel.setVisible(true);
	
		int xCoord = (int)(DisplayAssembler.getScreenWidth() - 
				basePanel.getPreferredSize().getWidth()) / 2;
		int yCoord = (int)(DisplayAssembler.getScreenHeight() - 
				basePanel.getPreferredSize().getHeight()) / 2;
		
		PopupFactory factory = PopupFactory.getSharedInstance();
		final Popup popup = factory.getPopup(this, basePanel, xCoord, yCoord);
		
		ok.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent event) {
	        	popup.hide();
	        	NotificationManager.getInstance().notifyObservers(Notification.REMOVE_CARD, null);
	        }
	
	    });
	
		popup.show();
		NotificationManager.getInstance().notifyObservers(Notification.SHOW_CARD, null);
	
    }

	public MessagePopUp(Card c, Player p, ArrayList<Player> players, String spaceName)
	{  	
		final Card card = c;
		final Player player = p;
		final ArrayList<Player> playerList = players;		
		
		Vector<String> lines = processText(card.getMessage());
		int numOfLabels = lines.size();
		
		JPanel basePanel = new JPanel();
		JPanel messagePanel = new JPanel();
		JLabel labels[] = new JLabel[numOfLabels];
				
		int hGap = 10, vGap = 10;
		GridLayout gridLayout = new GridLayout(2, 1, hGap, vGap);
		GridLayout gridLayout2 = new GridLayout(numOfLabels, 1, 0, 0);
		
		basePanel.setLayout(null);
		messagePanel.setLayout(null);
		
		Font labelFont = new Font("SansSerif", Font.BOLD, 20);
		Font buttonFont = new Font("SansSerif", Font.BOLD, 18);
		Font titleFont = new Font("SansSerif", Font.BOLD, 14);
		
		for (int i = 0; i < numOfLabels; ++i)
		{
			labels[i]  = new JLabel(lines.elementAt(i));
			labels[i].setFont(labelFont);
			labels[i].setForeground(Color.white);
			messagePanel.add(labels[i]);
		}

		JButton ok = new JButton("OK");
		ok.setFont(buttonFont);
		ok.setVisible(true);
		basePanel.setLayout(gridLayout);
		basePanel.setBackground(new Color(0, 0, 0));
		messagePanel.setLayout(gridLayout2);
		messagePanel.setBackground(new Color(0, 0, 0));
		
		Border blackline = BorderFactory.createLineBorder(Color.white, 2);
		TitledBorder title = BorderFactory.createTitledBorder(
			       blackline, "  " + spaceName + "  ");
	
		title.setTitleColor(Color.white);
		title.setTitleFont(titleFont);
		title.setTitleJustification(TitledBorder.LEFT);
		basePanel.setBorder(title);
		basePanel.add(messagePanel);
		basePanel.add(ok);
		basePanel.setVisible(true);
	
		int xCoord = (int)(DisplayAssembler.getScreenWidth() - 
				basePanel.getPreferredSize().getWidth()) / 2;
		int yCoord = (int)(DisplayAssembler.getScreenHeight() - 
				basePanel.getPreferredSize().getHeight()) / 2;
		
		PopupFactory factory = PopupFactory.getSharedInstance();
		final Popup popup = factory.getPopup(this, basePanel, xCoord, yCoord);
		
		ok.addActionListener(new ActionListener()
		{
	        public void actionPerformed(ActionEvent event) 
	        {
	        	popup.hide();
	        	NotificationManager.getInstance().notifyObservers(Notification.REMOVE_CARD, null);
	        	card.landOn(player, playerList);
	        }
	        
	    });

		popup.show();	
    }
	
	private Vector<String> processText(String message) {

		int maxpos = 40;
		
		Vector<String> trimmedWords = new Vector<String>(30);
		Vector<String> lines = new Vector<String>(5);

		StringTokenizer words = new StringTokenizer(message);
		while (words.hasMoreTokens())
		{
			trimmedWords.addElement(words.nextToken());
		}

		int count = 0;
		String temp = null;
		for (int i = 0; i < trimmedWords.size(); i = i + 1)
		{
			String currentWord = trimmedWords.elementAt(i);
			if(currentWord.length() > maxpos)
			{
				System.out.print("Error: word found longer than " + maxpos + " characters. Exiting.");
				return null;
			}
			
			if (count == 0)
			{
				temp = currentWord;
				count = currentWord.length();
			}
			
			else if ((count + currentWord.length() + 1) > maxpos)
			{
				lines.add(temp);
				temp = currentWord;
				count = currentWord.length();
			}

			else
			{
				temp += " " + currentWord;
				count = count + currentWord.length() + 1;
			}
		}
		lines.add(temp);
		return lines;
	}

}
